package projectdemo01.operation;

import projectdemo01.book.Book;
import projectdemo01.book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:咭咭熊
 * Date:2021 - 09 - 23
 * Time:19:48
 */

/***
 * 删除书籍
 */
public class DelOperation implements IOperation{

    @Override
    public void work(BookList bookList) {
        System.out.println("删除书籍");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的图书名字：");
        String bookname = scanner.nextLine();
        //(1)循环遍历查找要删除 图书名：
        int i = 0;
        for (; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(bookname)){
                break;
            }
        }
        if(i >= bookList.getUsedSize()){
            System.out.println("没有要删除的图书");
            return;
        }
        //循环因break退出，说明找打了这本书，对其进行删除
        int pos= i;
        for (int j = pos; j < bookList.getUsedSize() - 1; j++) {
            Book book = bookList.getBook(j + 1);
            bookList.setBooks(j,book);
        }
        int currentSize = bookList.getUsedSize();
        bookList.setUsedSize(currentSize - 1);
        System.out.println("删除书籍成功！");
    }
}
